<?php


function www($value, $useVarDump = false, $showTrace = false)
{
    $value = $value;
    $showTrace = $showTrace;
    echo PHP_EOL . (is_ajax() ? "/*" : '<pre>' . PHP_EOL);
    if ($useVarDump) {
        if (!is_ajax() && $useVarDump == 2) {
            $value = htmlspecialchars($value);
        }
        var_dump($value);
    } else {
        print_r($value);
    }
    if ($showTrace) {
        $e = new Exception();
        echo $e->getTraceAsString();
    }
    echo PHP_EOL . (is_ajax() ? "*/" : '</pre>') . PHP_EOL;
}


/**
 * Show dump in the table
 * @param $value
 * @return string
 */
function wwwTable($value, $echo = true)
{
    $echo;
    if (count($value) == 0) {
        return;
    }
    $str = '<table border="1">';
    $cnt = 0;
    foreach ($value as $i => $row) {
        $str .= "<tr>";
        if (is_array($row) || is_object($row)) {
            if ($cnt == 0 && ++$cnt) {
                foreach ($row as $ii => $rrow) {
                    $str .= "<td>";
                    $str .= ($ii ? $ii : "&nbsp;");
                    $str .= "</td>";
                }
                $str .= "</tr>";
                $str .= "<tr>";
            }
            foreach ($row as $ii => $rrow) {
                $str .= "<td>";
                $str .= ($rrow ? $rrow : "&nbsp;");
                $str .= "</td>";
            }
        } else {
            $str .= "<td>";
            $str .= ($row ? $row : "&nbsp;");
            $str .= "</td>";
        }
        $str .= "</tr>";
    }
    $str .= "</table>";
    if ($echo == true) {
        echo $str;
    }

    return $str;
}


function www1($value, $useVarDump = false, $showTrace = false) {
    www($value, $useVarDump, $showTrace);
    exit;
}
/**
 * Dump SQL with formater
 *
 * @param $sql          SQL-request
 * @return string
 */
function wwwSql($sql, $echo = true)
{
    $sql = str_replace(PHP_EOL, null,$sql);
    $sql = preg_replace('/[ |\t]{1,}/', ' ', $sql);
//    $sql = str_ireplace('SELECT ', PHP_EOL . "SELECT ", $sql);
    $sql = str_ireplace(' WHERE ', PHP_EOL . " WHERE ", $sql);
    $sql = str_ireplace('UNION', PHP_EOL . PHP_EOL . "UNION" . PHP_EOL . PHP_EOL, $sql);
    $sql = str_ireplace(' ORDER ', PHP_EOL . " ORDER ", $sql);
    $sql = str_ireplace(' GROUP ', PHP_EOL . " GROUP ", $sql);
    $sql = str_ireplace(' LEFT ', PHP_EOL . " LEFT ", $sql);
    $sql = str_ireplace(' FROM ', PHP_EOL . " FROM ", $sql);

    $sql = str_ireplace(' AND ', PHP_EOL . "     AND ", $sql);
    $sql = str_ireplace(' OR ', PHP_EOL . "     OR ", $sql);
    $sql = str_ireplace(',', "," . PHP_EOL ."    ", $sql);
    if ($echo) {
        if (is_ajax()) {
            echo "\n/*\n";
            echo $sql;
            echo "\n*/\n";
        } else {
            echo '<pre>';
            echo $sql;
            echo '</pre>';
        }
    }
    return $sql;
}

/**
 * Check ajax request
 *
 * @return bool
 */
function is_ajax()
{
    return (bool) strtolower(@ $_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' || @$_POST['ajax'];
}
